﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.Utility;
using System.Configuration;

namespace Artech.EnterLibExt.ExceptionHandling
{
    public abstract class PropertyComparisonFilter: IExceptionFilter
    {
        public string PropertyName { get; private set; }

        public PropertyComparisonFilter(string propertyName)
        {
            Guard.ArgumentNotNullOrEmpty(propertyName, "propertyName");
            this.PropertyName = propertyName;
        }

        public virtual object  GetPropertyValue(Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");
            var property = exception.GetType().GetProperty(this.PropertyName);
            if (null == property)
            { 
                throw new ConfigurationErrorsException(string.Format("There is no public \"{0}\" property defined in \"{1}\"." ,this.PropertyName, exception.GetType().FullName));
            }
            return property.GetValue(exception, null);
        }

        public abstract bool Match(Exception ex);
    }
}
